package service;

import beans.User;
import dbutils.DBHelper;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminService {
    //顾客管理
    public JTable createCustomerJTable() {
        // 创建表头
        String[] columnNames = {"ID", "用户名", "手机号", "密码", "余额"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM customer";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("cid");
                String name = resultSet.getString("cname");
                String phone = resultSet.getString("cphone");
                String password = resultSet.getString("cpassword");
                double balance = resultSet.getDouble("cmoney");


                // 添加数据行
                Object[] rowData = {id, name, phone, password, balance};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //添加顾客
    public void addCustomer() {
        JTextField nameTextField = new JTextField();
        JTextField phoneTextField = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        JTextField balanceTextField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);
        panel.add(new JLabel("余额："));
        panel.add(balanceTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "添加顾客信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameTextField.getText();
            String phone = phoneTextField.getText();
            String password = new String(passwordField.getPassword());
            // 解析余额值
            double balance;
            String balanceText = balanceTextField.getText();
            if (balanceText.isEmpty()) {
                balance = 0;
            } else {
                balance = Double.parseDouble(balanceText);
            }


            if (name.isEmpty() || phone.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "添加失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM customer WHERE cname = ? OR cphone = ?";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, name);
                selectStat.setString(2, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "添加失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement insertStat = null;
            String insertSql = "INSERT INTO customer (cname, cphone, cpassword, cmoney) VALUES (?, ?, ?, ?)";

            try {
                conn = DBHelper.getConn();
                insertStat = conn.prepareStatement(insertSql);
                insertStat.setString(1, name);
                insertStat.setString(2, phone);
                insertStat.setString(3, password);
                insertStat.setDouble(4, balance); // 设置余额值
                insertStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "添加顾客成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "新增顾客" + name);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, insertStat, null);
            }
        }
    }


    //修改顾客信息
    public void updateCustomer(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的顾客", "修改失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);
        String password = (String) table.getValueAt(row, 3);
        double balance = (double) table.getValueAt(row, 4);

        JTextField nameTextField = new JTextField(name);
        JTextField phoneTextField = new JTextField(phone);
        JTextField passwordField = new JTextField(password);
        JTextField balanceTextField = new JTextField(String.valueOf(balance));

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);
        panel.add(new JLabel("余额："));
        panel.add(balanceTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改顾客信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String newName = nameTextField.getText();
            String newPhone = phoneTextField.getText();
            String newPassword = passwordField.getText();
            double newBalance;

            // 如果没有填入修改的余额信息，则按照原信息来不变
            String balanceValue = balanceTextField.getText();
            if (balanceValue.isEmpty()) {
                newBalance = balance;
            } else {
                newBalance = Double.parseDouble(balanceValue);
            }

            if (newName.isEmpty() || newPhone.isEmpty() || newPassword.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "修改失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM customer WHERE (cname = ? OR cphone = ?) AND (cname != ? OR cphone != ?)";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, newName);
                selectStat.setString(2, newPhone);
                selectStat.setString(3, name);
                selectStat.setString(4, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "修改失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement updateStat = null;
            String updateSql = "UPDATE customer SET cname = ?, cphone = ?, cpassword = ?, cmoney = ? WHERE cname = ? AND cphone = ?";

            try {
                conn = DBHelper.getConn();
                updateStat = conn.prepareStatement(updateSql);
                updateStat.setString(1, newName);
                updateStat.setString(2, newPhone);
                updateStat.setString(3, newPassword);
                updateStat.setDouble(4, newBalance);
                updateStat.setString(5, name);
                updateStat.setString(6, phone);
                updateStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "修改顾客成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "修改了顾客" + name + "信息");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, updateStat, null);
            }
        }
    }


    //删除顾客信息
    public void deleteCustomer(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的顾客", "删除失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);

        int result = JOptionPane.showConfirmDialog(null, "确定要删除顾客 " + name + " 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            Connection conn = null;
            PreparedStatement deleteStat = null;
            String deleteSql = "DELETE FROM customer WHERE cname = ? AND cphone = ?";

            try {
                conn = DBHelper.getConn();
                deleteStat = conn.prepareStatement(deleteSql);
                deleteStat.setString(1, name);
                deleteStat.setString(2, phone);
                deleteStat.executeUpdate();

                JOptionPane.showMessageDialog(null, "删除顾客成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "删除了顾客" + name);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, deleteStat, null);
            }
        }
    }


    //查询顾客
    public void searchCustomerTable(JTable table, String keyword) {
        // 清空表格数据
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM customer WHERE cname LIKE ?";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            selectStat.setString(1, "%" + keyword + "%");
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                Object[] rowData = new Object[5];
                rowData[0] = resultSet.getInt("cid");
                rowData[1] = resultSet.getString("cname");
                rowData[2] = resultSet.getString("cphone");
                rowData[3] = resultSet.getString("cpassword");
                rowData[4] = resultSet.getDouble("cmoney");

                // 添加数据行
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //刷新顾客列表
    public void refreshCustomerTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM customer";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("cid");
                String name = resultSet.getString("cname");
                String phone = resultSet.getString("cphone");
                String password = resultSet.getString("cpassword");
                double money = resultSet.getDouble("cmoney");

                Object[] rowData = {id, name, phone, password, money};
                model.addRow(rowData); // 添加数据到表格模型
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //商家管理
    public JTable createMerchantJTable() {
        // 创建表头
        String[] columnNames = {"ID", "用户名", "手机号", "密码", "余额"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM merchant";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("mid");
                String name = resultSet.getString("mname");
                String phone = resultSet.getString("mphone");
                String password = resultSet.getString("mpassword");
                String balance = resultSet.getString("mmoney");


                // 添加数据行
                Object[] rowData = {id, name, phone, password, balance};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //添加商家
    public void addMerchant() {
        JTextField nameTextField = new JTextField();
        JTextField phoneTextField = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        JTextField balanceTextField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);
        panel.add(new JLabel("余额："));
        panel.add(balanceTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "添加商家信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameTextField.getText();
            String phone = phoneTextField.getText();
            String password = new String(passwordField.getPassword());
            // 解析余额值
            double balance;
            String balanceText = balanceTextField.getText();
            if (balanceText.isEmpty()) {
                balance = 0;
            } else {
                balance = Double.parseDouble(balanceText);
            }


            if (name.isEmpty() || phone.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "添加失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM merchant WHERE mname = ? OR mphone = ?";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, name);
                selectStat.setString(2, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "添加失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement insertStat = null;
            String insertSql = "INSERT INTO merchant (mname, mphone, mpassword, mmoney) VALUES (?, ?, ?, ?)";

            try {
                conn = DBHelper.getConn();
                insertStat = conn.prepareStatement(insertSql);
                insertStat.setString(1, name);
                insertStat.setString(2, phone);
                insertStat.setString(3, password);
                insertStat.setDouble(4, balance); // 设置余额值
                insertStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "添加商家成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "新增商家 " + name);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, insertStat, null);
            }
        }
    }


    //修改商家信息
    public void upadteMerchant(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的商家", "修改失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);
        String password = (String) table.getValueAt(row, 3);
        double balance = (double) table.getValueAt(row, 4);

        JTextField nameTextField = new JTextField(name);
        JTextField phoneTextField = new JTextField(phone);
        JTextField passwordField = new JTextField(password);
        JTextField balanceTextField = new JTextField(String.valueOf(balance));

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);
        panel.add(new JLabel("余额："));
        panel.add(balanceTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改商家信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String newName = nameTextField.getText();
            String newPhone = phoneTextField.getText();
            String newPassword = passwordField.getText();
            double newBalance;

            // 如果没有填入修改的余额信息，则按照原信息来不变
            String balanceValue = balanceTextField.getText();
            if (balanceValue.isEmpty()) {
                newBalance = balance;
            } else {
                newBalance = Double.parseDouble(balanceValue);
            }

            if (newName.isEmpty() || newPhone.isEmpty() || newPassword.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "修改失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM merchant WHERE (mname = ? OR mphone = ?) AND (mname != ? OR mphone != ?)";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, newName);
                selectStat.setString(2, newPhone);
                selectStat.setString(3, name);
                selectStat.setString(4, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "修改失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement updateStat = null;
            String updateSql = "UPDATE merchant SET mname = ?, mphone = ?, mpassword = ?, mmoney = ? WHERE mname = ? AND mphone = ?";

            try {
                conn = DBHelper.getConn();
                updateStat = conn.prepareStatement(updateSql);
                updateStat.setString(1, newName);
                updateStat.setString(2, newPhone);
                updateStat.setString(3, newPassword);
                updateStat.setDouble(4, newBalance);
                updateStat.setString(5, name);
                updateStat.setString(6, phone);
                updateStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "修改商家成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "修改了商家 " + name +" 信息");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, updateStat, null);
            }
        }
    }


    //删除商家信息
    public void deleteMerchant(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的商家", "删除失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);

        int result = JOptionPane.showConfirmDialog(null, "确定要删除商家 " + name + " 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            Connection conn = null;
            PreparedStatement deleteStat = null;
            String deleteSql = "DELETE FROM merchant WHERE mname = ? AND mphone = ?";

            try {
                conn = DBHelper.getConn();
                deleteStat = conn.prepareStatement(deleteSql);
                deleteStat.setString(1, name);
                deleteStat.setString(2, phone);
                deleteStat.executeUpdate();

                JOptionPane.showMessageDialog(null, "删除商家成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "删除了商家 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, deleteStat, null);
            }
        }
    }


    //查询商家
    public void searchMerchantTable(JTable table, String keyword) {
        // 清空表格数据
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM merchant WHERE mname LIKE ?";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            selectStat.setString(1, "%" + keyword + "%");
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                Object[] rowData = new Object[5];
                rowData[0] = resultSet.getInt("mid");
                rowData[1] = resultSet.getString("mname");
                rowData[2] = resultSet.getString("mphone");
                rowData[3] = resultSet.getString("mpassword");
                rowData[4] = resultSet.getDouble("mmoney");

                // 添加数据行
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //刷新生产商列表
    public void refreshMerchantTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM merchant";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("mid");
                String name = resultSet.getString("mname");
                String phone = resultSet.getString("mphone");
                String password = resultSet.getString("mpassword");
                double money = resultSet.getDouble("mmoney");

                Object[] rowData = {id, name, phone, password, money};
                model.addRow(rowData); // 添加数据到表格模型
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //生产商管理
    public JTable createProducerJTable() {
        // 创建表头
        String[] columnNames = {"ID", "用户名", "手机号", "密码"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM producer";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("pid");
                String name = resultSet.getString("pname");
                String phone = resultSet.getString("pphone");
                String password = resultSet.getString("ppassword");

                // 添加数据行
                Object[] rowData = {id, name, phone, password};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //添加生产商
    public void addProducer() {
        JTextField nameTextField = new JTextField();
        JTextField phoneTextField = new JTextField();
        JPasswordField passwordField = new JPasswordField();


        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);

        int result = JOptionPane.showConfirmDialog(null, panel, "添加生产商信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameTextField.getText();
            String phone = phoneTextField.getText();
            String password = new String(passwordField.getPassword());

            if (name.isEmpty() || phone.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "添加失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM producer WHERE pname = ? OR pphone = ?";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, name);
                selectStat.setString(2, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "添加失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement insertStat = null;
            String insertSql = "INSERT INTO producer (pname, pphone, ppassword) VALUES ( ?, ?, ?)";

            try {
                conn = DBHelper.getConn();
                insertStat = conn.prepareStatement(insertSql);
                insertStat.setString(1, name);
                insertStat.setString(2, phone);
                insertStat.setString(3, password);
                insertStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "添加生产商成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "新增生产商 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, insertStat, null);
            }
        }
    }


    //修改生产商信息
    public void upadteProducer(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的生产商", "修改失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);
        String password = (String) table.getValueAt(row, 3);

        JTextField nameTextField = new JTextField(name);
        JTextField phoneTextField = new JTextField(phone);
        JTextField passwordField = new JTextField(password);

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("用户名："));
        panel.add(nameTextField);
        panel.add(new JLabel("手机号："));
        panel.add(phoneTextField);
        panel.add(new JLabel("密码："));
        panel.add(passwordField);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改商家信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String newName = nameTextField.getText();
            String newPhone = phoneTextField.getText();
            String newPassword = passwordField.getText();

            if (newName.isEmpty() || newPhone.isEmpty() || newPassword.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名、手机号和密码不能为空", "修改失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM producer WHERE (pname = ? OR pphone = ?) AND (pname != ? OR pphone != ?)";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, newName);
                selectStat.setString(2, newPhone);
                selectStat.setString(3, name);
                selectStat.setString(4, phone);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "用户名或手机号已存在", "修改失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement updateStat = null;
            String updateSql = "UPDATE producer SET pname = ?, pphone = ?, ppassword = ? WHERE pname = ? AND pphone = ?";

            try {
                conn = DBHelper.getConn();
                updateStat = conn.prepareStatement(updateSql);
                updateStat.setString(1, newName);
                updateStat.setString(2, newPhone);
                updateStat.setString(3, newPassword);
                updateStat.setString(4, name);
                updateStat.setString(5, phone);
                updateStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "修改生产商成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "修改了生产商 " + name + " 信息");

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, updateStat, null);
            }
        }
    }


    //删除生产商信息
    public void deleteProducer(JTable table) {
        // 获取选中的顾客信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的生产商", "删除失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String phone = (String) table.getValueAt(row, 2);

        int result = JOptionPane.showConfirmDialog(null, "确定要删除生产商 " + name + " 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            Connection conn = null;
            PreparedStatement deleteStat = null;
            String deleteSql = "DELETE FROM producer WHERE pname = ? AND pphone = ?";

            try {
                conn = DBHelper.getConn();
                deleteStat = conn.prepareStatement(deleteSql);
                deleteStat.setString(1, name);
                deleteStat.setString(2, phone);
                deleteStat.executeUpdate();

                JOptionPane.showMessageDialog(null, "删除生产商成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "删除了生产商 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, deleteStat, null);
            }
        }
    }


    //查询生产商
    public void searchProducerTable(JTable table, String keyword) {
        // 清空表格数据
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM producer WHERE pname LIKE ?";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            selectStat.setString(1, "%" + keyword + "%");
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                Object[] rowData = new Object[4];
                rowData[0] = resultSet.getInt("pid");
                rowData[1] = resultSet.getString("pname");
                rowData[2] = resultSet.getString("pphone");
                rowData[3] = resultSet.getString("ppassword");

                // 添加数据行
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //刷新生产商列表
    public void refreshProducerTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM producer";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("pid");
                String name = resultSet.getString("pname");
                String phone = resultSet.getString("pphone");
                String password = resultSet.getString("ppassword");

                Object[] rowData = {id, name, phone, password};
                model.addRow(rowData); // 添加数据到表格模型
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //创建商品管理
    public JTable createShoeJTable() {
        // 创建表头
        String[] columnNames = {"ID", "商品名称", "数量", "价格", "尺码", "商家", "生产商", "状态"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM shoe";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("sid");
                String sname = resultSet.getString("sname");
                int scount = resultSet.getInt("scount");
                double sprice = resultSet.getDouble("sprice");
                double ssize = resultSet.getDouble("ssize");
                String smerchant = resultSet.getString("smerchant");
                String sproducer = resultSet.getString("sproducer");
                String sstatus = resultSet.getString("sstatus");

                // 添加数据行
                Object[] rowData = {id, sname, scount, sprice, ssize, smerchant, sproducer, sstatus};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //添加商品
    public void addShoe() {
        JTextField nameTextField = new JTextField();
        JTextField countTextField = new JTextField();
        JTextField priceTextField = new JTextField();
        JTextField sizeTextField = new JTextField();
        JTextField merchantTextField = new JTextField();
        JTextField producerTextField = new JTextField();
        String[] statusOptions = {"已上架", "已下架"};
        JComboBox<String> statusComboBox = new JComboBox<>(statusOptions);

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("商品名："));
        panel.add(nameTextField);
        panel.add(new JLabel("数量："));
        panel.add(countTextField);
        panel.add(new JLabel("价格："));
        panel.add(priceTextField);
        panel.add(new JLabel("尺码："));
        panel.add(sizeTextField);
        panel.add(new JLabel("商家："));
        panel.add(merchantTextField);
        panel.add(new JLabel("生产商："));
        panel.add(producerTextField);
        panel.add(new JLabel("状态："));
        panel.add(statusComboBox);


        int result = JOptionPane.showConfirmDialog(null, panel, "添加商品信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameTextField.getText();
            String count = countTextField.getText();
            String price = priceTextField.getText();
            String size = sizeTextField.getText();
            String merchant = merchantTextField.getText();
            String producer = producerTextField.getText();
            String status = (String) statusComboBox.getSelectedItem();

            if (name.isEmpty() || count.isEmpty() || price.isEmpty() || size.isEmpty() || merchant.isEmpty()) {
                JOptionPane.showMessageDialog(null, "商品名、数量、价格、尺码和商家不能为空", "添加失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM shoe WHERE sname = ?";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, name);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "商品名已存在", "添加失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement insertStat = null;
            String insertSql = "INSERT INTO shoe (sname, scount, sprice, ssize, smerchant, sproducer, sstatus) VALUES (?, ?, ?, ?, ?, ?, ?)";

            try {
                conn = DBHelper.getConn();
                insertStat = conn.prepareStatement(insertSql);
                insertStat.setString(1, name);
                insertStat.setString(2, count);
                insertStat.setString(3, price);
                insertStat.setString(4, size);
                insertStat.setString(5, merchant);
                insertStat.setString(6, producer);
                insertStat.setString(7, status);
                insertStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "添加商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "新增商品 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, insertStat, null);
            }
        }
    }

    //修改商品信息
    public void upadteShoe(JTable table) {
        // 获取选中的商品信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的商品", "修改失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String count = String.valueOf(table.getValueAt(row, 2));
        String price = String.valueOf(table.getValueAt(row, 3));
        String size = String.valueOf(table.getValueAt(row, 4));
        String merchant = (String) table.getValueAt(row, 5);
        String producer = (String) table.getValueAt(row, 6);
        String status = (String) table.getValueAt(row, 7);

        JTextField nameTextField = new JTextField(name);
        JTextField countTextField = new JTextField(count);
        JTextField priceTextField = new JTextField(price);
        JTextField sizeTextField = new JTextField(size);
        JTextField merchantTextField = new JTextField(merchant);
        JTextField producerTextField = new JTextField(producer);
        String[] statusOptions = {"已上架", "已下架"};
        JComboBox<String> statusComboBox = new JComboBox<>(statusOptions);
        statusComboBox.setSelectedItem(status);

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("商品名："));
        panel.add(nameTextField);
        panel.add(new JLabel("数量："));
        panel.add(countTextField);
        panel.add(new JLabel("价格："));
        panel.add(priceTextField);
        panel.add(new JLabel("尺码："));
        panel.add(sizeTextField);
        panel.add(new JLabel("商家："));
        panel.add(merchantTextField);
        panel.add(new JLabel("生产商："));
        panel.add(producerTextField);
        panel.add(new JLabel("状态："));
        panel.add(statusComboBox);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改商品信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String newName = nameTextField.getText();
            String newCount = countTextField.getText();
            String newPrice = priceTextField.getText();
            String newSize = sizeTextField.getText();
            String newMerchant = merchantTextField.getText();
            String newProducer = producerTextField.getText();
            String newStatus = (String) statusComboBox.getSelectedItem();

            if (newName.isEmpty() || newCount.isEmpty() || newPrice.isEmpty() || newSize.isEmpty() || newMerchant.isEmpty()) {
                JOptionPane.showMessageDialog(null, "商品名、数量、价格、尺码和商家不能为空", "修改失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement selectStat = null;
            ResultSet resultSet = null;
            String selectSql = "SELECT * FROM shoe WHERE sname = ? AND sid != ?";

            try {
                conn = DBHelper.getConn();
                selectStat = conn.prepareStatement(selectSql);
                selectStat.setString(1, newName);
                selectStat.setInt(2, row + 1);
                resultSet = selectStat.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(null, "商品名已存在", "修改失败", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, selectStat, resultSet);
            }

            PreparedStatement updateStat = null;
            String updateSql = "UPDATE shoe SET sname = ?, scount = ?, sprice = ?, ssize = ?, smerchant = ?, sproducer = ?, sstatus = ? WHERE sid = ?";

            try {
                conn = DBHelper.getConn();
                updateStat = conn.prepareStatement(updateSql);
                updateStat.setString(1, newName);
                updateStat.setString(2, newCount);
                updateStat.setString(3, newPrice);
                updateStat.setString(4, newSize);
                updateStat.setString(5, newMerchant);
                updateStat.setString(6, newProducer);
                updateStat.setString(7, newStatus);
                updateStat.setInt(8, row + 1);
                updateStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "修改商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "修改商品 " + name + " 信息");

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, updateStat, null);
            }
        }
    }

    //删除商品信息
    public void deleteShoe(JTable table) {
        // 获取选中的商品信息
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的商品", "删除失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);

        int result = JOptionPane.showConfirmDialog(null, "确定要删除商品 " + name + " 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            Connection conn = null;
            PreparedStatement deleteStat = null;
            String deleteSql = "DELETE FROM shoe WHERE sid = ?";

            try {
                conn = DBHelper.getConn();
                deleteStat = conn.prepareStatement(deleteSql);
                deleteStat.setInt(1, row + 1);
                deleteStat.executeUpdate();

                JOptionPane.showMessageDialog(null, "删除商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "删除商品 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, deleteStat, null);
            }
        }
    }



    //查询商品
    public void searchShoeTable(JTable table, String keyword) {
        // 清空表格数据
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM shoe WHERE sname LIKE ?";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            selectStat.setString(1, "%" + keyword + "%");
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                Object[] rowData = new Object[8];
                rowData[0] = resultSet.getInt("sid");
                rowData[1] = resultSet.getString("sname");
                rowData[2] = resultSet.getInt("scount");
                rowData[3] = resultSet.getDouble("sprice");
                rowData[4] = resultSet.getDouble("ssize");
                rowData[5] = resultSet.getString("smerchant");
                rowData[6] = resultSet.getString("sproducer");
                rowData[7] = resultSet.getString("sstatus");
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }

    //刷新商品
    public void refreshShoeTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM shoe";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int sid = resultSet.getInt("sid");
                String sname = resultSet.getString("sname");
                int scount = resultSet.getInt("scount");
                double sprice = resultSet.getDouble("sprice");
                double ssize = resultSet.getDouble("ssize");
                String smerchant = resultSet.getString("smerchant");
                String sproducer = resultSet.getString("sproducer");
                String sstatus = resultSet.getString("sstatus");

                Object[] rowData = {sid, sname, scount, sprice, ssize, smerchant, sproducer, sstatus};
                model.addRow(rowData); // 添加数据到表格模型
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    // 仓库管理面板表格
    public JTable createWarehouseJTable() {
        // 创建表头
        String[] columnNames = {"ID", "名称", "数量", "价格", "尺寸", "商家", "产商"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM warehouse";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("wid");
                String name = resultSet.getString("wname");
                String count = resultSet.getString("wcount");
                double price = resultSet.getDouble("wprice");
                double size = resultSet.getDouble("wsize");
                String merchant = resultSet.getString("wmerchant");
                String producer = resultSet.getString("wproducer");

                // 添加数据行
                Object[] rowData = {id, name, count, price, size, merchant, producer};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    // 添加仓库商品
    public void addWarehouseItem() {
        JTextField nameTextField = new JTextField();
        JTextField countTextField = new JTextField();
        JTextField priceTextField = new JTextField();
        JTextField sizeTextField = new JTextField();
        JTextField merchantTextField = new JTextField();
        JTextField producerTextField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("商品名称："));
        panel.add(nameTextField);
        panel.add(new JLabel("库存数量："));
        panel.add(countTextField);
        panel.add(new JLabel("价格："));
        panel.add(priceTextField);
        panel.add(new JLabel("尺寸："));
        panel.add(sizeTextField);
        panel.add(new JLabel("商家："));
        panel.add(merchantTextField);
        panel.add(new JLabel("生产商："));
        panel.add(producerTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "添加仓库商品信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameTextField.getText();
            String count = countTextField.getText();
            String price = priceTextField.getText();
            String size = sizeTextField.getText();
            String merchant = merchantTextField.getText();
            String producer = producerTextField.getText();

            if (name.isEmpty() || count.isEmpty() || price.isEmpty() || size.isEmpty() || merchant.isEmpty() || producer.isEmpty()) {
                JOptionPane.showMessageDialog(null, "商品信息不能为空", "添加失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement insertStat = null;
            String insertSql = "INSERT INTO warehouse (wname, wcount, wprice, wsize, wmerchant, wproducer) VALUES (?, ?, ?, ?, ?, ?)";

            try {
                conn = DBHelper.getConn();
                insertStat = conn.prepareStatement(insertSql);
                insertStat.setString(1, name);
                insertStat.setString(2, count);
                insertStat.setString(3, price);
                insertStat.setString(4, size);
                insertStat.setString(5, merchant);
                insertStat.setString(6, producer);
                insertStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "添加仓库商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "添加库存商品 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, insertStat, null);
            }
        }
    }

    // 修改仓库商品信息
    public void updateWarehouseItem(JTable table) {
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的仓库商品", "修改失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);
        String count = (String) table.getValueAt(row, 2);
        String price = (String) table.getValueAt(row, 3);
        String size = (String) table.getValueAt(row, 4);
        String merchant = (String) table.getValueAt(row, 5);
        String producer = (String) table.getValueAt(row, 6);

        JTextField nameTextField = new JTextField(name);
        JTextField countTextField = new JTextField(count);
        JTextField priceTextField = new JTextField(price);
        JTextField sizeTextField = new JTextField(size);
        JTextField merchantTextField = new JTextField(merchant);
        JTextField producerTextField = new JTextField(producer);

        JPanel panel = new JPanel(new GridLayout(0, 2));
        panel.add(new JLabel("商品名称："));
        panel.add(nameTextField);
        panel.add(new JLabel("库存数量："));
        panel.add(countTextField);
        panel.add(new JLabel("价格："));
        panel.add(priceTextField);
        panel.add(new JLabel("尺寸："));
        panel.add(sizeTextField);
        panel.add(new JLabel("商家："));
        panel.add(merchantTextField);
        panel.add(new JLabel("生产商："));
        panel.add(producerTextField);

        int result = JOptionPane.showConfirmDialog(null, panel, "修改仓库商品信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String newName = nameTextField.getText();
            String newCount = countTextField.getText();
            String newPrice = priceTextField.getText();
            String newSize = sizeTextField.getText();
            String newMerchant = merchantTextField.getText();
            String newProducer = producerTextField.getText();

            if (newName.isEmpty() || newCount.isEmpty() || newPrice.isEmpty() || newSize.isEmpty() || newMerchant.isEmpty() || newProducer.isEmpty()) {
                JOptionPane.showMessageDialog(null, "商品信息不能为空", "修改失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Connection conn = null;
            PreparedStatement updateStat = null;
            String updateSql = "UPDATE warehouse SET wname = ?, wcount = ?, wprice = ?, wsize = ?, wmerchant = ?, wproducer = ? WHERE wname = ?";

            try {
                conn = DBHelper.getConn();
                updateStat = conn.prepareStatement(updateSql);
                updateStat.setString(1, newName);
                updateStat.setString(2, newCount);
                updateStat.setString(3, newPrice);
                updateStat.setString(4, newSize);
                updateStat.setString(5, newMerchant);
                updateStat.setString(6, newProducer);
                updateStat.setString(7, name);
                updateStat.executeUpdate();
                JOptionPane.showMessageDialog(null, "修改仓库商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "修改了库存商品 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, updateStat, null);
            }
        }
    }

    // 删除仓库商品信息
    public void deleteWarehouseItem(JTable table) {
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的仓库商品", "删除失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String name = (String) table.getValueAt(row, 1);

        int result = JOptionPane.showConfirmDialog(null, "确定要删除仓库商品 " + name + " 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            Connection conn = null;
            PreparedStatement deleteStat = null;
            String deleteSql = "DELETE FROM warehouse WHERE wname = ?";

            try {
                conn = DBHelper.getConn();
                deleteStat = conn.prepareStatement(deleteSql);
                deleteStat.setString(1, name);
                deleteStat.executeUpdate();

                JOptionPane.showMessageDialog(null, "删除仓库商品成功");
                //添加日志
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "删除了库存商品 " + name);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBHelper.closeAll(conn, deleteStat, null);
            }
        }
    }


    // 查询仓库商品
    public void searchWarehouseTable(JTable table, String keyword) {
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);

        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM warehouse WHERE wname LIKE ?";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            selectStat.setString(1, "%" + keyword + "%");
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                Object[] rowData = new Object[6];
                rowData[0] = resultSet.getInt("wid");
                rowData[1] = resultSet.getString("wname");
                rowData[2] = resultSet.getString("wcount");
                rowData[3] = resultSet.getDouble("wprice");
                rowData[4] = resultSet.getDouble("wsize");
                rowData[5] = resultSet.getString("wmerchant");

                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }

    // 刷新仓库商品列表
    public void refreshWarehouseTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM warehouse";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("wid");
                String name = resultSet.getString("wname");
                String count = resultSet.getString("wcount");
                double price = resultSet.getDouble("wprice");
                double size = resultSet.getDouble("wsize");
                String merchant = resultSet.getString("wmerchant");

                Object[] rowData = {id, name, count, price, size, merchant};
                model.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }



    //订单管理
    public JTable createOrderJTable() {
        // 创建表头
        String[] columnNames = {"订单号", "用户", "时间", "商品名称", "商品价格", "商家", "生产商"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM corders";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int orderNum = resultSet.getInt("orderNum");
                String user = resultSet.getString("user");
                String times = resultSet.getString("times");
                String name = resultSet.getString("name");
                String price = resultSet.getString("price");
                String merchant = resultSet.getString("merchant");
                String producer = resultSet.getString("producer");

                // 添加数据行
                Object[] rowData = {orderNum, user, times, name, price, merchant, producer};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //刷新订单
    public void refreshOrderTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM corders";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                int orderNum = resultSet.getInt("orderNum");
                String user = resultSet.getString("user");
                String times = resultSet.getString("times");
                String name = resultSet.getString("name");
                String price = resultSet.getString("price");
                String merchant = resultSet.getString("merchant");
                String producer = resultSet.getString("producer");

                Object[] rowData = {orderNum, user, times, name, price, merchant, producer};
                model.addRow(rowData); // 添加数据到表格模型

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }


    //日志管理
    public JTable createLogJTable() {
        // 创建表头
        String[] columnNames = {"用户名", "时间", "日志内容"};

        // 创建 JTable 对象
        JTable table = new JTable();

        // 设置表头
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setColumnIdentifiers(columnNames);

        // 查询数据库数据并添加到表格中
        Connection conn = DBHelper.getConn();
        String sql = "SELECT * FROM logs";
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;

        try {
            selectStat = conn.prepareStatement(sql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                String userName = resultSet.getString("userName");
                String times = resultSet.getString("times");
                String logContext = resultSet.getString("logContext");

                // 添加数据行
                Object[] rowData = {userName, times, logContext};
                tableModel.addRow(rowData);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }

        return table;
    }


    //刷新日志
    public void refreshLogTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0); // 清空表格数据

        Connection conn = null;
        PreparedStatement selectStat = null;
        ResultSet resultSet = null;
        String selectSql = "SELECT * FROM logs";

        try {
            conn = DBHelper.getConn();
            selectStat = conn.prepareStatement(selectSql);
            resultSet = selectStat.executeQuery();

            while (resultSet.next()) {
                String userName = resultSet.getString("userName");
                String times = resultSet.getString("times");
                String logContext = resultSet.getString("logContext");

                Object[] rowData = {userName, times, logContext};
                model.addRow(rowData); // 添加数据到表格模型
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, selectStat, resultSet);
        }
    }
}
